package com.example.rentingapp.fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;

import com.example.rentingapp.R;
import com.example.rentingapp.activity.OrderDetailsActivity1;
import com.example.rentingapp.activity.OrderDetailsActivity3;
import com.example.rentingapp.adapter.OrderAdapter;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.OrdersEntity;
import com.example.rentingapp.enetity.OrdersResponse;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Successful_Order_Fragment extends BaseFragment {
    private OrderAdapter orderAdapter = null;
    private ListView listView;
    private List<OrdersEntity> mOrderEntity1 = new ArrayList<>(), mOrderEntity2 = new ArrayList<>(), mOrderEntity = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                        mOrderEntity.clear();
                        mOrderEntity.addAll(mOrderEntity1);
                        mOrderEntity.addAll(mOrderEntity2);
                        orderAdapter = new OrderAdapter(mOrderEntity, getContext());
                        listView.setAdapter(orderAdapter);
                        orderAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.fragment_successful_order;
    }

    @Override
    protected void initView() {
        listView = mRootView.findViewById(R.id.listview2);
    }

    @Override
    protected void initData() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                removeByKey("orders_id");
                String orders_id = String.valueOf(mOrderEntity.get(position).getOrders_id());
                insertVal("orders_id", orders_id);//将订单id存入本地

                transfer(OrderDetailsActivity3.class, (int) orderAdapter.getItemId(position));
                getActivity().finish();
            }
        });
        findOrderByUserId();
    }

    //通过userid查找订单
    private void findOrderByUserId() {
        String userId = findByKey("user_id");
        char check ='Y';
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("orders_landlord_id", userId);
        params.put("check_orders",check);
        //作为房东
        Api.config(ApiConfig.FIND_ORDERS_BY_LANDLORD_ID_AND_CHECK, params).postRequest(getActivity(), new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                OrdersResponse ordersResponse = gson.fromJson(res, OrdersResponse.class);
                if (ordersResponse.getSuccess() == true) {
                    List<OrdersEntity> list1 = ordersResponse.getDatas();
                    if (list1 != null && list1.size() > 0) {
                        mOrderEntity1 = list1;
                    }
                } else {
//                    showToastSync("暂无房东订单");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
        params.put("orders_tenant_id", userId);
        params.put("check_orders",check);
        //作为房客
        Api.config(ApiConfig.FIND_ORDERS_BY_TENANT_ID_AND_CHECK, params).postRequest(getActivity(), new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                OrdersResponse orderResponse = gson.fromJson(res, OrdersResponse.class);
                if (orderResponse.getSuccess() == true) {
                    List<OrdersEntity> list2 = orderResponse.getDatas();
                    if (list2 != null && list2.size() > 0) {
                        mOrderEntity2 = list2;
                    }
                } else {
//                    showToastSync("暂无房客订单");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
        mHandler.sendEmptyMessage(0);
    }
}

